
package com.airplane.hotdiscuss.network;

import com.airplane.hotdiscuss.util.Constants;
import com.airplane.hotdiscuss.util.MyLogger;

import java.util.HashMap;
import java.util.Map;

public class MyHttpRequest {
    private final MyLogger logger = MyLogger.getLogger(MyHttpRequest.class.getSimpleName());

    private int mRequestType;
    private String mRequestBodyString;
    private byte[] mRequestBody;
    private String mContentType = Constants.MIME_TEXT_XML;
    private String mContentEncoding = Constants.ENCODING_UTF8;
    private String mUrl;
    private String mProxyHost;
    private int mProxyPort;
    private boolean mIsPostMethod;
    private Map<String, String> mUrlParams = new HashMap<String, String>();
    private Map<String, String> mHeaders = new HashMap<String, String>();

    public int getRequestType() {
        return mRequestType;
    }

    public void setRequestType(int mRequestType) {
        this.mRequestType = mRequestType;
    }

    public String getRequestBodyString() {
        return mRequestBodyString;
    }

    public void setRequestBodyString(String mRequestBodyString) {
        this.mRequestBodyString = mRequestBodyString;
    }

    public byte[] getRequestBody() {
        return mRequestBody;
    }

    public void setRequestBody(byte[] mRequestBody) {
        this.mRequestBody = mRequestBody;
    }

    public String getContentType() {
        return mContentType;
    }

    public void setContentType(String mContentType) {
        this.mContentType = mContentType;
    }

    public String getContentEncoding() {
        return mContentEncoding;
    }

    public void setContentEncoding(String mContentEncoding) {
        this.mContentEncoding = mContentEncoding;
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String mUrl) {
        this.mUrl = mUrl;
    }

    public String getProxyHost() {
        return mProxyHost;
    }

    public void setProxyHost(String mProxyHost) {
        this.mProxyHost = mProxyHost;
    }

    public int getProxyPort() {
        return mProxyPort;
    }

    public void setProxyPort(int mProxyPort) {
        this.mProxyPort = mProxyPort;
    }

    public boolean isPostMethod() {
        return mIsPostMethod;
    }

    public void setPostMethod(boolean mIsPostMethod) {
        this.mIsPostMethod = mIsPostMethod;
    }

    public Map<String, String> getUrlParams() {
        return mUrlParams;
    }

    public void setUrlParams(Map<String, String> mUrlParams) {
        this.mUrlParams = mUrlParams;
    }

    public Map<String, String> getHeaders() {
        return mHeaders;
    }

    public void setHeaders(Map<String, String> mHeaders) {
        this.mHeaders = mHeaders;
    }

    public void addHeader(String name, String value) {
        mHeaders.put(name, value);
    }

    public void addUrlParams(String name, String value) {
        mUrlParams.put(name, value);
    }

    public String getUrlWithParams() {
        // set url params
        String url = getUrl();

        if (url == null) {
            return null;
        }

        boolean first = url.indexOf('?') == -1;
        StringBuilder buf = new StringBuilder(url);
        for (String key : mUrlParams.keySet()) {
            String value = mUrlParams.get(key);

            if (first) {
                buf.append('?');
            } else if (buf.charAt(buf.length() - 1) != '?') {
                buf.append('&');
            }
            buf.append(key).append('=').append(value);

            first = false;

            url = buf.toString();
        }
        return url;
    }
}
